//
//  AMapManager.m
//  Unity-iPhone
//
//  Created by Brian on 2019/2/28.
//

#import "BMapManager.h"

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>

#import "UUIDTool.h"

#import <MJExtension/MJExtension.h>

@interface BMapManager () <AMapLocationManagerDelegate>

typedef void (^LocatingCompletionBlock)(CLLocation *location, NSError *error);

@property (nonatomic, strong) AMapLocationManager *locManager;

@property (nonatomic) CGFloat lat;
@property (nonatomic) CGFloat lon;
@property (nonatomic, copy) LocatingCompletionBlock locaBlock;

@end

@implementation BMapManager

char * _Getgps()
{
//    {"Lat":"123.123","Lon":"456.456","device":"1df8f8d8f"}
    [BMapManager sharedSingleton];
    if ([BMapManager sharedSingleton].lat != 0 && [BMapManager sharedSingleton].lon != 0) {
        
        NSDictionary *info = @{
                               @"Lat":[NSNumber numberWithDouble:[BMapManager sharedSingleton].lat].stringValue,
                               @"Lon":[NSNumber numberWithDouble:[BMapManager sharedSingleton].lon].stringValue,
                               @"device":[UUIDTool getUUIDInKeychain],
                               };
        NSString *gps = [info mj_JSONString];
        const char * _output = [gps UTF8String];

        // 是里如果直接返回_output是会报错的，因为它是是const，不能被返回。
        char* res = (char*)malloc(strlen(_output)+1);
        strcpy(res, _output);
        
        return res;
    }
    else {
            NSDictionary *info = @{
                                   @"Lat":[NSNumber numberWithDouble:[BMapManager sharedSingleton].lat].stringValue,
                                   @"Lon":[NSNumber numberWithDouble:[BMapManager sharedSingleton].lon].stringValue,
                                   @"device":[UUIDTool getUUIDInKeychain],
                                   };
            NSString *gps = [info mj_JSONString];
        const char * _output = [gps UTF8String];
        
        // 是里如果直接返回_output是会报错的，因为它是是const，不能被返回。
        char* res = (char*)malloc(strlen(_output)+1);
        strcpy(res, _output);
        
        return res;

    }
    
}

+ (instancetype)sharedSingleton {
    static BMapManager *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [BMapManager sharedSingleton];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [BMapManager sharedSingleton];
}

- (instancetype)init {
    if (self = [super init]) {
        [self configLocationManager];
    }
    return self;
}

- (void)configLocationManager
{
    [[AMapServices sharedServices] setEnableHTTPS:YES];
    [AMapServices sharedServices].apiKey = @"3ef20818d9a18d121390132cf5e6e4e1";

    self.locManager = [[AMapLocationManager alloc] init];
    [self.locManager setDelegate:self];
    [self.locManager setPausesLocationUpdatesAutomatically:NO];
//    [self.locManager setAllowsBackgroundLocationUpdates:YES];
    [self startSerialLocation];
}

- (void)startSerialLocation
{
    //开始定位
    [self.locManager startUpdatingLocation];
}

- (void)stopSerialLocation
{
    //停止定位
    [self.locManager stopUpdatingLocation];
}

- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    //定位错误
    NSLog(@"%s, amapLocationManager = %@, error = %@", __func__, [manager class], error);
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
    //定位结果
//    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
    self.lat = location.coordinate.latitude;
    self.lon = location.coordinate.longitude;
}


@end
